import { Buffer } from 'buffer';
import { ZipFile } from 'yazl';

export interface IFile {
	file: Buffer;
	path: string;
}

/**
 * 解压包
 * @param files [IFile[]] 需要解压的文件
 * @example zip(files);
 */
export default function zip(...files: IFile[]) {
	return new Promise<Buffer>((resolve, _reject) => {
		const zipfile = new ZipFile();
		files.forEach((f) => {
			zipfile.addBuffer(f.file, f.path);
		});
		const data = [] as Buffer[];
		zipfile.outputStream.on('data', (chunk: Buffer) => {
			data.push(chunk);
		});
		zipfile.outputStream.on('end', () => {
			resolve(Buffer.concat(data));
		});
		zipfile.end();
	});
}
